package org.jeecg.modules.controller.patient;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.annotation.Anonymous;
import org.jeecg.common.annotation.Anonymous;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.bean.entity.patient.Patient;
import org.jeecg.modules.service.patient.IPatientService;
import org.jeecg.modules.utils.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: patient
 * @Author: jeecg-boot
 * @Date:   2023-11-09
 * @Version: V1.0
 */
@Api(tags="用户信息查询")
@RestController
@RequestMapping("/patient/patient")
@Slf4j
public class PatientController extends JeecgController<Patient, IPatientService> {
	@Autowired
	private IPatientService patientService;
	
	/**
	 * 分页列表查询
	 *
	 * @param patient
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="patient-分页列表查询", notes="patient-分页列表查询")
	@GetMapping(value = "/list")
	public Result<List<Patient>> queryPageList(Patient patient,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											   HttpServletRequest req) {
		List<Patient> pageList = patientService.getUserPage(patient, pageNo, pageSize, req);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param patient
	 * @return
	 */
	@AutoLog(value = "patient-添加")
	@ApiOperation(value="patient-添加", notes="patient-添加")
//	@RequiresPermissions("patient:patient:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Patient patient) {
		patientService.save(patient);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param patient
	 * @return
	 */
	@AutoLog(value = "patient-编辑")
	@ApiOperation(value="patient-编辑", notes="patient-编辑")
//	@RequiresPermissions("patient:patient:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Patient patient) {
		patientService.updateById(patient);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "patient-通过id删除")
	@ApiOperation(value="patient-通过id删除", notes="patient-通过id删除")
	@RequiresPermissions("patient:patient:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		patientService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "patient-批量删除")
	@ApiOperation(value="patient-批量删除", notes="patient-批量删除")
	@RequiresPermissions("patient:patient:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.patientService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "patient-通过id查询")
	@ApiOperation(value="patient-通过id查询", notes="patient-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Patient> queryById(@RequestParam(name="id",required=true) String id) {
		Patient patient = patientService.getById(id);
		if(patient==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(patient);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param patient
    */
    @RequiresPermissions("patient:patient:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Patient patient) {
        return super.exportXls(request, patient, Patient.class, "patient");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("patient:patient:importExcel")
    @PostMapping(path = "/importExcel")
    public Result<String> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Patient.class);
    }


    @ApiOperation(value = "识别身份证", notes = "识别身份证")
    @GetMapping(value = "/queryPerson")
    public Result<Patient> queryPerson() throws UnsupportedEncodingException, ParseException {
        Person person = new Person("D:\\Downloads");
        Patient patient = person.open();
		if (null == patient.getCertificateno()) {
			return Result.error("请刷入身份证信息！！");
		}
        return Result.OK(patient);
    }
}
